<?php
namespace M\Action;
use Think\Page;
use Common\Model\userModel;
class UserAction extends BaseAction {
	
	public function _initialize() {
	parent::_initialize();
	$this->modorder=M('order');
    $useragent = strtolower(addslashes($_SERVER['HTTP_USER_AGENT'])); 
	if(strpos($useragent, 'micromessenger')>0 && C('yh_site_tiaozhuan')==1){
		$this->weixinlogin();
	}else{
		
	if($this->visitor->is_login == false){
			$url=U('login/index','','');
			redirect($url);
		}
	}
		
		$this->artmod=M('article')->cache(true, 10 * 60);
		
		$user=$this->visitor->get();
		
		$this->assign('user', $user);
		
//		if($user['webmaster'] == 1){
//			$this->redirect('zhan/ucenter');
//		}
		if($user['tbname'] == 1){
		  $this->assign('isagent',true);
		}
		
		
	}

public function myorder(){
	
	    $p = I('p', 1, 'intval');
	    $status = I('status', 0, 'intval');
		$page_size = 6;
		$start = $page_size * ($p - 1);
		if($status){
		$stay['status'] = $status;
		$this->assign('status',$status);
		}
		
		$stay['uid'] = $this->memberinfo['id'];
		$stay['nstatus'] = 1;
		$rows = $this->modorder->where($stay)->order('id desc')->limit($start . ',' . $page_size)->select();
		$count = $this->modorder->where($stay)->count('1');
		$pager = new Page($count, $page_size);
		$this->assign('page', $pager->show());
		$this->assign('total_item', $count);
		$this -> assign('page_size',$page_size);
		$list=array();
		$fuid=$this->memberinfo['fuid'];
		$guid=$this->memberinfo['guid'];
		foreach($rows as $k=>$v){
			$list[$k]['status']=$this->orderstatic($v['status']);
			$list[$k]['state']=$v['status'];
			$list[$k]['orderid']=$v['orderid'];
			$list[$k]['goods_title']=$v['goods_title'];
			$list[$k]['add_time']=$v['add_time'];
			$list[$k]['price']=$v['price'];
			$list[$k]['bask']=$v['bask'];
			$list[$k]['income']=Orebate1(array('price'=>$v['income'],'leve'=>$v['leve1']));
			$list[$k]['up_time']=$v['up_time'];
			$list[$k]['id']=$v['id'];
		}
		
		$this->assign('list',$list);
		
	
$this->_config_seo(array(
			'title'=>'我的订单'
			)); 
$this->display();	
}

public function channelorder(){
	    $p = I('p', 1, 'intval');
	    $status = I('status', 0, 'intval');
		$page_size = 10;
		$start = $page_size * ($p - 1);
		if($status){
		$stay['status'] = $status;
		$this->assign('status',$status);
		}
		$stay['relation_id'] = $this->memberinfo['webmaster_pid'];
		$stay['nstatus'] = 2;
		$rows = $this->modorder->field('goods_title,goods_num',true)->where($stay)->order('id desc')->limit($start . ',' . $page_size)->select();
		$count = $this->modorder->where($stay)->count('1');
		$pager = new Page($count, $page_size);
		$this->assign('page', $pager->show());
		$this->assign('total_item', $count);
		$this -> assign('page_size',$page_size);
		$list=array();
		$fuid=$this->memberinfo['fuid'];
		$guid=$this->memberinfo['guid'];
		foreach($rows as $k=>$v){
			$list[$k]['status']=$this->orderstatic($v['status']);
			$list[$k]['state']=$v['status'];
			$list[$k]['orderid']=$v['orderid'];
			$list[$k]['add_time']=$v['add_time'];
			$list[$k]['goods_title']=$v['goods_title'];
			$list[$k]['price']=$v['price'];
			$list[$k]['bask']=$v['bask'];
			$list[$k]['income']= round($v['income']*($this->memberinfo['webmaster_rate']/100), 2);
			$list[$k]['up_time']=$v['up_time'];
			$list[$k]['id']=$v['id'];
		}
		
		$this->assign('list',$list);
		
	
$this->_config_seo(array(
			'title'=>'渠道订单'
			)); 
$this->display();	
}

	public function vieworder(){
		
		$this->display();
		
	}

	public function fillphone(){
		//已作废
		if(IS_POST){
			$phone = I('phone','','trim');
			$exist = M('user')->where(array(
				'phone' => $phone
				))->count('id');
			
			if ($exist) {
				return $this->ajaxReturn(0, '手机号已存在！');
			}
			$data['phone'] = $phone;
			$res=M('user')->where('id='.$this->visitor->get('id'))->save($data);
			if($res){
				return $this->ajaxReturn(1, '成功保存！');
			}else{
				return $this->ajaxReturn(0, '保存失败！');
				
			}
			
		}


     $track_val=cookie('trackid'); 
     if(!empty($track_val)){
        	$track=unserialize($track_val);
        	$uid=$track['t'];
        	$this->assign('recode',$track['recode']);
    	}

$this->_config_seo(array(
            'title'=>'绑定手机号'
            )); 

		$this->display();	
		
	}
	


public function qrcode(){
		
if(C('APP_SUB_DOMAIN_DEPLOY')){
		$headerm_html = str_replace('/index.php/m','',trim(C('yh_headerm_html')));
		$data = $headerm_html.'/?t='.$this->visitor->get('id');
}else{
		$data = trim(C('yh_headerm_html')).'?t='.$this->visitor->get('id');
}

		$level = 'L';  
		$size = 4;
		vendor("phpqrcode.phpqrcode");
		$object = new \QRcode();
		ob_clean();
		$object->png($data, false, $level, $size,0);
	}


public function ucenter(){
$NowTime=NOW_TIME;
$bindtime = F('history/bindtime');
$moduser=M('user');
if(($NowTime - $bindtime)>180){
	$this->bindorder();
	F('history/bindtime', $NowTime);
}
		$uid=$this->memberinfo['id'];
		if(!$uid){
		$url=U('login/index','','');
		redirect($url);
		}
		
		if($uid>0){
		$code=$this->visitor->get('invocode');
		if(!$code){
		$codes=$this->invicode($uid);
		$this->assign('invicode',$codes);
		}
		}
		
		$tbname=$this->visitor->get('tbname');
		if(1 != $tbname){
		$map=array(
		'uid'=>$uid,
		'nstatus'=>1,
		'status'=>3
		);
		$mymoney=$this->modorder->where($map)->sum('price');
		$agentcondition=trim(C('yh_agentcondition'));
		if(!$mymoney)$mymoney=0;
		if($mymoney>=$agentcondition){
		$moduser->where(array('id'=>$uid))->setField('tbname',1);	
		}
			
		}
		if($tbname == 1 && C('yh_qrcode') == 1){
		$qrname='qrcode_'.$uid;
		$image = F($qrname);
		if(!$image){
		$apiurl='http://app.tuiquanke.cn/follow?uid='.$this->tqkuid.'&tuid='.$uid.'&time='.NOW_TIME;
		$qrcodeinfo=$this->_curl($apiurl);
		$images = $this->getImage($qrcodeinfo);
	    F($qrname,$images['save_path']);
	    $image=$images['save_path'];
		}
		$this->assign('qrcode',$image);
		}
		$article=$this->artmod->where('cate_id=2')->field('id,title')->limit(5)->select();
		$this->assign('article',$article);	
		$where=array(
			'oid'=>$this->visitor->get('oid'),
			'uid'=>$uid,
			'status'=>1
			);

$start=strtotime(date('Y-m-01', strtotime('last day of -1 month')));	
$end =strtotime(date('Y-m-01', strtotime(date("Y-m-d"))));

if($this->memberinfo['webmaster_pid'] && $this->memberinfo['webmaster'] == 1){
$web_field='
( select SUM(income) from tqk_order where nstatus=2 and (status =1 or status =3) and relation_id = '.$this->memberinfo['webmaster_pid'].' and add_time>'.$end.') as relation_month,
( select SUM(income) from tqk_order where nstatus=2 and (status =1 or status =3) and relation_id = '.$this->memberinfo['webmaster_pid'].' and (add_time>'.$start.' and add_time<'.$end.') ) as relation_premonth,
';	
}

$field = 'id,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and uid = '.$uid.' and (up_time>'.$start.' and up_time<'.$end.') ) as u_premonth,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and fuid = '.$uid.' and (up_time>'.$start.' and up_time<'.$end.') ) as f_premonth,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and guid = '.$uid.' and (up_time>'.$start.' and up_time<'.$end.') ) as g_premonth,
'.$web_field.'
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and uid = '.$uid.' and up_time>'.$end.') as u_month,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and fuid = '.$uid.' and up_time>'.$end.') as f_month,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and guid = '.$uid.' and up_time>'.$end.') as g_month';
$res = $moduser->field($field)->find();
$pre_money=Rebate1($res['u_premonth'])+Rebate2($res['f_premonth'])+Rebate3($res['g_premonth'])+$res['relation_premonth']*($this->memberinfo['webmaster_rate']/100);
$this_money=Rebate1($res['u_month'])+Rebate2($res['f_month'])+Rebate3($res['g_month'])+$res['relation_month']*($this->memberinfo['webmaster_rate']/100);
$this->assign('pre_money',$pre_money?$pre_money:0);
$this->assign('this_money',$this_money?$this_money:0);
		
		$this->assign('r',$this->memberinfo['id']);
		$this->_config_seo(array(
			'title'=>'用户中心'
			)); 
		$this->display('ucenter');
	}

	protected function orderstatic($id){
		switch($id){
			case 0 :
			return '待处理';
			break;
			case 1 :
			return '已付款';
			break;
			case 2 :
			return '无效订单';
			break;
			case 3 :
			return '已结算';
			break;
			default : 
			return '订单失效';
			break;
		}
		
	}


public function teamprofit(){
$modfin=M('finance');
$uid=$this->memberinfo['id'];
$fuid=$this->memberinfo['fuid'];
$guid=$this->memberinfo['guid'];
$today_str = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$tomorr_str = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$pre_time = strtotime(date('Y-m-01', strtotime('last day of -1 month')));
$last_str = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
$pre_month=$this->getthemonth($pre_time);


if($this->memberinfo['webmaster_pid'] && $this->memberinfo['webmaster'] == 1){
$web_field='
( select SUM(income) from tqk_order where nstatus=2 and (status =1 or status =3) and relation_id = '.$this->memberinfo['webmaster_pid'].' and (add_time>'.$last_str.' and add_time<'.$today_str.') ) as relation_lastmoney,
( select SUM(income) from tqk_order where nstatus=2 and (status =1 or status =3) and relation_id = '.$this->memberinfo['webmaster_pid'].' and up_time>'.$pre_month[1].') as relation_month,
( select count(id) from tqk_order where nstatus=2 and (status =1 or status =3) and relation_id = '.$this->memberinfo['webmaster_pid'].' and add_time>'.$today_str.') as relation_today_count,
( select count(id) from tqk_order where nstatus=2 and (status =1 or status =3) and relation_id = '.$this->memberinfo['webmaster_pid'].' and add_time>'.$pre_month[1].' ) as relation_month_count,
( select count(id) from tqk_order where nstatus=2 and (status =1 or status =3) and relation_id = '.$this->memberinfo['webmaster_pid'].' and (add_time>'.$pre_month[0].' and add_time<'.$pre_month[1].') ) as relation_last_month_count,
';
}


$field = 'id,
( select SUM(income) from tqk_finance where uid = '.$uid.' and type=3) as allmoney,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and uid = '.$uid.' and (add_time>'.$last_str.' and add_time<'.$today_str.') ) as u_lastmoney,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and fuid = '.$uid.' and (add_time>'.$last_str.' and add_time<'.$today_str.') ) as f_lastmoney,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and guid = '.$uid.' and (add_time>'.$last_str.' and add_time<'.$today_str.') ) as g_lastmoney,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and uid = '.$uid.' and up_time>'.$pre_month[1].') as u_month,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and fuid = '.$uid.' and up_time>'.$pre_month[1].') as f_month,
( select SUM(income) from tqk_order where nstatus=1 and (status =1 or status =3) and guid = '.$uid.' and up_time>'.$pre_month[1].') as g_month,
'.$web_field.'
( select count(id) from tqk_order where nstatus=1 and (status =1 or status =3) and (uid = '.$uid.' or fuid='.$uid.' or guid='.$uid.') and add_time>'.$today_str.') as today_count,
( select count(id) from tqk_order where nstatus=1 and (status =1 or status =3) and (uid = '.$uid.' or fuid='.$uid.' or guid='.$uid.') and add_time>'.$pre_month[1].' ) as this_month_count,
( select count(id) from tqk_order where nstatus=1 and (status =1 or status =3) and (uid = '.$uid.' or fuid='.$uid.' or guid='.$uid.') and (add_time>'.$pre_month[0].' and add_time<'.$pre_month[1].') ) as last_month_count';
$res = M('user')->field($field)->find();
if($res){

$allmoney=$res['allmoney'];

$this->assign('allmoney',$allmoney);
	
	
$yesterday_money=Rebate1($res['u_lastmoney'])+Rebate2($res['f_lastmoney'])+Rebate3($res['g_lastmoney'])+$res['relation_lastmoney']*($this->memberinfo['webmaster_rate']/100);
$this->assign('yesterday_money',$yesterday_money);

$this_month=Rebate1($res['u_month'])+Rebate2($res['f_month'])+Rebate3($res['g_month'])+$res['relation_month']*($this->memberinfo['webmaster_rate']/100);
$this->assign('this_month',$this_month);

	
}

$this->assign('stat',$res);

$p = I('p', 1, 'intval');
$page_size = 6;
$start = $page_size * ($p - 1);
$stay['uid'] = $uid;
$stay['type'] = 3;
$rows = $modfin->where($stay)->order('id desc')->limit($start . ',' . $page_size)->select();
		$count = $modfin->where($stay)->count();
		$pager = new Page($count, $page_size);
		$this->assign('page', $pager->show());
		$this->assign('total_item', $count);
		$this -> assign('page_size',$page_size);
		$list=array();
		foreach($rows as $k=>$v){
			$list[$k]['status']=$this->Fstatic($v['status']);
			$list[$k]['mark']=$v['mark'];
			$list[$k]['price']=$v['price'];
			$list[$k]['add_time']=$v['add_time'];
			$list[$k]['backcash']=$v['backcash'];
			$list[$k]['income']=$v['income'];
			$list[$k]['id']=$v['id'];
		}

$this->assign('list',$list);


$this->_config_seo(array(
			'title'=>'我的收益'
			)); 
$this->display();

}
	

public function teamorder(){
$p = I('p', 1, 'intval');
$uid=$this->memberinfo['id'];
$fuid = I('fuid', 0, 'intval');
$guid = I('guid', 0, 'intval');
$page_size = 10;
$start = $page_size * ($p - 1);

if($fuid){
$stay['fuid'] = $uid;	
$this->assign('fuid',$fuid);
}

if($guid){
$stay['guid'] = $uid;	
$this->assign('guid',$guid);
}

if(!$guid && !$fuid){
$stay['fuid'] = $uid;	
$this->assign('fuid',1);	
}
//$stay['nstatus'] = 1;
$rows = $this->modorder->field('status,orderid,add_time,price,income,up_time,id,leve1,leve2,leve3')->where($stay)->order('id desc')->limit($start . ',' . $page_size)->select();
//
$count = $this->modorder->where($stay)->count();

$pager = new Page($count, $page_size);
$this->assign('page', $pager->show());
$this->assign('total_item', $count);
$this -> assign('page_size',$page_size);

$list=array();
		foreach($rows as $k=>$v){
			$list[$k]['status']=$this->orderstatic($v['status']);
			$list[$k]['state']=$v['status'];
			$list[$k]['orderid']=$v['orderid'];
			$list[$k]['add_time']=$v['add_time'];
			$list[$k]['price']=$v['price'];
			$list[$k]['income']=$v['income'];
			$list[$k]['up_time']=$v['up_time'];
			$list[$k]['id']=$v['id'];
			$list[$k]['leve1']=$v['leve1'];
			$list[$k]['leve2']=$v['leve2'];
			$list[$k]['leve3']=$v['leve3'];
		}
		
		$this->assign('list',$list);
	
$this->_config_seo(array(
			'title'=>'团队订单'
			)); 
		
$this->display();		
}
	
	

public function myteam(){
$usermod=M('user');
$uid=$this->memberinfo['id'];
$today_str = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$last_str = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
        $field = 'id,
        ( select count(id) from tqk_user where fuid = '.$uid.' or guid = '.$uid.') as allperson,
        ( select count(id) from tqk_user where (fuid = '.$uid.' or guid = '.$uid.') and reg_time>'.$today_str.' ) as todayperson,
        ( select count(id) from tqk_user where (fuid = '.$uid.' or guid = '.$uid.') and (reg_time>'.$last_str.' and reg_time<'.$today_str.') ) as lastperson,
        ( select count(id) from tqk_user where fuid = '.$uid.') as person1,
        ( select count(id) from tqk_user where guid = '.$uid.') as person2';
        $res = $usermod->field($field)->find();
        $this->assign('stat',$res);

$p = I('p', 1, 'intval');
$fuid = I('fuid', 0, 'intval');
$guid = I('guid', 0, 'intval');
$page_size = 10;
$start = $page_size * ($p - 1);

if($fuid){
$stay['fuid'] = $uid;	
$this->assign('fuid',$fuid);
}

if($guid){
$stay['guid'] = $uid;	
$this->assign('guid',$guid);
}

if(!$guid && !$fuid){
$stay['fuid'] = $uid;	
$this->assign('fuid',1);	
}

$rows = $usermod->field('reg_time,nickname,oid')->where($stay)->order('id desc')->limit($start . ',' . $page_size)->select();
$count = $usermod->where($stay)->count();
$pager = new Page($count, $page_size);
$this->assign('page', $pager->show());
$this->assign('total_item', $count);
$this->assign('info',$rows);
$this -> assign('page_size',$page_size);


	
$this->_config_seo(array(
			'title'=>'我的团队'
			)); 
		
$this->display();	
	
}


	public function order(){
		$p = I('p', 1, 'intval');
		$page_size = 10;
		$mod=M('basklistlogo');
		$start = $page_size * ($p - 1);
		$stay['uid'] = $this->memberinfo['id'];
		$rows = $mod->where($stay)->order('id desc')->limit($start . ',' . $page_size)->select();
		$count = $mod->where($stay)->count('1');
		$pager = new Page($count, $page_size);
		$this->assign('page', $pager->show());
		$this->assign('total_item', $count);
		$this -> assign('page_size',$page_size);
		$this->assign('list',$rows);
		
		$where=array(
			'oid'=>$this->visitor->get('oid'),
			'uid'=>$this->memberinfo['id'],
			'status'=>1
			);
		
		$integral=$this->modorder->where($where)->sum('integral');
		
		$this->assign('integral',$integral?$integral:0);
		
		$this->_config_seo(array(
			'title'=>'积分兑换'
			)); 
		
		$this->display('invite');	
		
		
	}


	public function jifen(){

		if(IS_POST){
			$count = I('count','','trim');
			if ($count<=0) {
				$this->ajaxReturn(0, '兑换数量与实际数量不符');
			}

			$user=M('user')->where('id='.$this->visitor->get('id'))->field('id,score,money')->find();

			if ($count>$user['score']) {
				$this->ajaxReturn(0, '兑换数量与实际数量不符');
			}

			$userid=$this->visitor->get('id');
			$price=(C('yh_fanxian')/100)*$count;

			if($price>0){
				
				M('user')->where('id='.$userid)->save(array(
					'money'=>array('exp','money+'.$price),
					'score'=>array('exp','score-'.$count)
					));

				M('usercash')->add(array(
					'uid'         =>$userid,
					'money'       =>$price,
					'remark'      =>'积分兑换: '.$price.'元',
					'type'        =>10, 
					'create_time' =>time(),
					'status'      =>1,
					));
				$this->ajaxReturn(1, '兑换成功！');

			}else{
				
				$this->ajaxReturn(0, '兑换失败！');
			}
			
		}



		$this->_config_seo(array(
			'title'=>'积分兑换'
			)); 
		
		$this->display();	
		
	}

public function suborder(){
$mod=M('user');
$this->_config_seo(array(
			'title'=>'提交淘宝订单'
			)); 
			
if(IS_POST){

$orderid = I('orderid','','trim');
if(is_numeric($orderid)){

$UID=$this->visitor->get('id');

$map=array(
'id'=>$UID,
);
$oid=md5(substr($orderid,-6,6));
$data=array(
'oid'=>$oid
);


$isset=$mod->field('id')->where($data)->find();

if(!$isset){
$res=M('user')->where($map)->save($data);
if($res){
return $this->ajaxReturn(1,'提交成功！');
}else{
$this->ajaxReturn(0,'提交失败，请检查后重试');	
}

}else{
$this->ajaxReturn(0,'此订单已经被其它账号绑定过！');		
}

}else{
$this->ajaxReturn(0,'提交的订单参数不符合要求');
	
}

}
			

$oid=$this->visitor->get('oid');	
if($oid){
$this->assign('oid',true);
}
			
			
		$this->display();	

	}

public function bindtb(){
$time=NOW_TIME;
$useragent = strtolower(addslashes($_SERVER['HTTP_USER_AGENT'])); 
if(strpos($useragent, 'micromessenger') > 1){ 
$wechat=1;
}
//
$UID=$this->visitor->get('id');

$map=array(
'uid'=>$UID,
'from'=>'wap'
);

$token=$this->create_token(trim(C('yh_gongju')),$map);

$url = U('bindtb/index',array('uid'=>$UID,'from'=>'wap','token'=>$token),'');

$this->assign('quanurl',$url);

$useragent = strtolower(addslashes($_SERVER['HTTP_USER_AGENT'])); 
if(strpos($useragent, 'micromessenger') > 1){ 
$map=array(
'uid'=>$UID,
'from'=>'wechat'
);
$token=$this->create_token(trim(C('yh_gongju')),$map);
$url=U('msg/bindtb',array('uid'=>$UID,'from'=>'wechat','token'=>$token),'');
redirect($url);

}else{
redirect($url);
}

//$this->display();
}
	
	public function index() {
		$callback=I('callback', '', 'trim');     
		$where['openid'] = $this->openid;
		$user = M('user')->where($where)->field('nickname,avatar,money,score,openid')->find();
		if($user){
			$this->assign('user',$user);				
		}else{
			exit('您的账号不存在！');
		}
		$this->_config_seo(array(
			'title'=>'用户中心'
			));                         
		$this->display();
	}
	
	public function modify() {
		
		$F=M('user');
		
		if(IS_POST){
			$password = I('password','','trim');
			$password2 = I('password2','','trim');
			$phone = I('phone','','trim');
			$data=array(
				'nickname'=>I('nickname','','trim'),
//				'username'=>I('username','trim'),
				'qq'=>I('qq','','trim'),
				'wechat'=>I('wechat','','trim'),
				);
			if(I('avatar','','trim')){
				$data['avatar'] = I('avatar','','trim');
			}
			
	if($phone){
	   $result = $F->where(array('phone'=>$phone))->count('id');
        if ($result) {
        $this->ajaxReturn(0,'手机号已存在');
        } else {
          $data['phone'] = $phone;
        }
	}
			
			
			if($password){
				if($password == $password2){
					$data['password'] = md5($password);
				}else{
					$this->ajaxReturn(0,'两次密码不一致');
				}
			}
			if($_FILES['avatar']){
				$file = $this->_upload($_FILES['avatar'], 'avatar',$thumb = array('width'=>150,'height'=>150));
				if($file['error']) {
					$this->ajaxReturn(0,$file['info']);
				} else {
					$data['avatar']=$file['mini_pic'];
				}
			}
			
			$where['id'] = $this->visitor->get('id');
			$res = $F->where($where)->save($data);
			
			if($res !== false){
				return $this->ajaxReturn(1,'修改成功');
			}
			$this->ajaxReturn(0,'修改失败');
		}
		$where['id'] = $this->visitor->get('id');
		$info = $F->where($where)->field('nickname,avatar,username,qq,wechat,phone')->find();
		$this->assign('info',$info);
		$this->_config_seo(array(
			'title'=>'修改资料'
			)); 
		$this->display();
	}
	
	public function tixian() {
		$mod=new userModel();
		if(IS_POST){
			$F=M('balance');
			$mymoney = abs(I('money','','trim'));
			if($mymoney<=0){
			$this->ajaxReturn(0,'提现金额异常！');
				exit();	
			}
			if($mymoney<C('yh_Quota')){
			$this->ajaxReturn(0,'单笔提现金额不能小于'.C('yh_Quota').'元');
				exit();	
			}
			$map['id'] = $this->visitor->get('id');
			$balance = $mod->field('nickname,avatar,username,money,id,realname,alipay')->where($map)->find();
			$alipay=I('allpay','','trim');
			$name=I('name','','trim');
			if($alipay && $name && !$balance['alipay']){
			$data=array(
			'realname'=>$name,
			'alipay'=>$alipay
			);
			$mod->where($map)->save($data);
			}
			
     		if($mymoney>$balance['money']){
     			$this->ajaxReturn(0,'账户余额不足！');
				exit();
     		}
     			$data=array(
					'uid'=>$balance['id'],
					'money'=>$mymoney,
					'name'=>$name?$name:$balance['realname'],
					'method'=>2,
					'allpay'=>$alipay?$alipay:$balance['alipay'],
					'status'=>0,
					'content'=>I('content','','trim'),
					'create_time'=>time()
				);
			$res = $F->add($data);
			if($res !== false){
			$mod->where(array(
                'id'=>$balance['id']
            ))->save(array(
                'money'=>array('exp','money-'.$mymoney),
                'frozen'=>array('exp','frozen+'.$mymoney),
));

M('usercash')->add(array(
                'uid'=>$balance['id'],
                'money'=>$mymoney,
                'type'=>1,
                'remark'=>'提现冻结资金：'.$mymoney.'元',
                'create_time'=>NOW_TIME,
                'status'=>1,
   ));
   $myphone=trim(C('yh_sms_my_phone'));
   if(1 == C('yh_sms_status') && $myphone){
     $data=array(
            'phone'=>$myphone,
            'code'=>$this->visitor->get('nickname'),
            'webname'=>$mymoney,
            'temp_id'=>trim(C('yh_sms_tixian_id'))
            );
  			 $res= $this->send_sms($data);
   }
			  return $this->ajaxReturn(1,'申请提交成功，请等待处理！');
	 }
				$this->ajaxReturn(0,'申请提交失败！');
		}
		 
	$where['id'] = $this->visitor->get('id');
    	$user = $mod->where($where)->field('nickname,avatar,username,money,realname,alipay')->find();
    	$this->assign('user',$user);
	    $this->_config_seo(array(
            'title'=>'我要提现'
        ));                         
      	$this->display();
			
			
		}
		
		public function journal() {
			$p = I('p', 1, 'intval');
			$page_size = 10;
			$start = $page_size * ($p - 1);
			$where['id'] = $this->visitor->get('id');
			$user = M('user')->where($where)->field('money,frozen,id')->find();
			if($user){
				$this->assign('user',$user);				
			}else{
				exit('您的账号不存在！');
			}
			
			$where=array(
				'type'=>6,
				'uid'=>$user['id']
				);
			$balance = M('usercash')->where($where)->sum('money');
			if($balance){
				$this->assign('balance',$balance);		
			}else{
				$this->assign('balance',0);		
			}
			$stay['uid'] = $user['id'];
			$rows = M('usercash')->where($stay)->field('type,money,create_time,status,remark')->order('id desc')->limit($start . ',' . $page_size)->select();
			$count = M('usercash')->where($stay)->count(1);
			$pager = new Page($count, $page_size);

			$this->assign('page', $pager->show());
			$this->assign('total_item', $count);
			$this -> assign('page_size',$page_size);
			$list=array();
			foreach($rows as $k=>$v){
				$val=unserialize(user_cash_type($v['type']));
				$list[$k]['create_time']=$v['create_time'];
				$list[$k]['type']=$val[0];
				$list[$k]['remark']=$v['remark'];
				$list[$k]['money']=$val[1].$v['money'];
			}
			$this->assign('info',$list);
			$this->_config_seo(array(
				'title'=>'财务日志'
				)); 
			$this->display();
		}
		
		public function record() {
			$p = I('p', 1, 'intval');
			$page_size = 10;
			$start = $page_size * ($p - 1);
			$where['id'] = $this->visitor->get('id');
			$user = M('user')->where($where)->field('money,frozen')->find();
			if($user){
				$this->assign('user',$user);				
			}else{
				exit('您的账号不存在！');
			}
			$map['uid'] = $this->visitor->get('id');
			$rows = M('balance')->where($map)->field('money,create_time,status')->limit($start . ',' . $page_size)->order('id desc')->select();
			$count = M('balance')->where($map)->count();
			$pager = new Page($count, $page_size);
			$this->assign('page', $pager->show());
			$this->assign('total_item', $count);
			$this -> assign('page_size',$page_size);
			$this->assign('info',$rows);
			$this->_config_seo(array(
				'title'=>'我的钱包'
				)); 
			$this->display();
		}
	}

